package com.dream.technology.service.impl;

import com.dream.technology.db.dao.RcClassificationMapper;
import com.dream.technology.db.entity.*;
import com.dream.technology.service.RcClassificationService;
import com.dream.technology.service.RcCustomerTagService;
import com.dream.technology.service.RcRuleCollectionService;
import com.dream.technology.service.RcRuleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("rcClassificationService")
public class RcClassificationServiceImpl implements RcClassificationService {

    @Autowired
    RcClassificationMapper rcClassificationMapper;

    @Autowired
    private RcCustomerTagService rcCustomerTagService;
    @Autowired
    private RcRuleService rcRuleService;
    @Autowired
    private RcRuleCollectionService rcRuleCollectionService;
    @Override
    public int countByExample(RcClassificationQuery example) {
        return rcClassificationMapper.countByExample(example);
    }

    @Override
    @Transactional
    public int deleteByExample(RcClassificationQuery example) {
        return rcClassificationMapper.deleteByExample(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(Long id) {
        return rcClassificationMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int insert(RcClassification record) {
        //获取管理员id

        record.setAdminId(Long.valueOf("1"));

        return rcClassificationMapper.insert(record);
    }

    @Override
    @Transactional
    public int insertSelective(RcClassification record) {
        return rcClassificationMapper.insertSelective(record);
    }

    @Override
    public List<RcClassification> selectByExample(RcClassificationQuery example) {
        return rcClassificationMapper.selectByExample(example);
    }

    @Override
    public RcClassification selectByPrimaryKey(Long id) {
        return rcClassificationMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByExampleSelective(RcClassification record, RcClassificationQuery example) {
        return rcClassificationMapper.updateByExampleSelective(record,example);
    }

    @Override
    @Transactional
    public int updateByExample(RcClassification record, RcClassificationQuery example) {
        return rcClassificationMapper.updateByExample(record,example);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(RcClassification record) {
    	RcClassification rcClassification = rcClassificationMapper.selectByPrimaryKey(record.getId());
    	int count = rcClassificationMapper.updateByPrimaryKeySelective(record);
    	// 修改分类名称时，修改对应的标签、规则、规则集引用的分类名称
    	if (count == 1) {
    		// 首先判断名称是否要改变
    		if (!rcClassification.getClassifyName().equals(record.getClassifyName())) {
    			// 查询引用此分类的标签、规则、规则集
    			if (rcClassification.getClassifyType() == 0) {
    				RcCustomerTagQuery tagQuery = new RcCustomerTagQuery();
    				RcCustomerTagQuery.Criteria criteria = tagQuery.createCriteria();
    				criteria.andTagTypeIdEqualTo(rcClassification.getId().intValue());
    				List<RcCustomerTag> tagList = rcCustomerTagService.selectByExample(tagQuery);
    				if (CollectionUtils.isNotEmpty(tagList)) {
    					for (RcCustomerTag tag : tagList) {
    						RcCustomerTag tagNew = new RcCustomerTag();
    						tagNew.setId(tag.getId());
    						tagNew.setTagTypeName(record.getClassifyName());
        					rcCustomerTagService.updateByPrimaryKeySelective(tagNew);
						}
    				}
    			}
    			if (rcClassification.getClassifyType() == 1) {
    				RcRuleQuery ruleQuery = new RcRuleQuery();
    				RcRuleQuery.Criteria criteria = ruleQuery.createCriteria();
    				criteria.andClassifyIdEqualTo(rcClassification.getId().intValue());
    				List<RcRule> ruleList = rcRuleService.selectByExample(ruleQuery);
    				if (CollectionUtils.isNotEmpty(ruleList)) {
    					for (RcRule rule : ruleList) {
    						RcRule ruleNew = new RcRule();
    						ruleNew.setId(rule.getId());
    						ruleNew.setClassifyName(record.getClassifyName());
    						rcRuleService.updateByPrimaryKeySelective(ruleNew);
						}
    				}
    			}
    			if (rcClassification.getClassifyType() == 2) {
    				RcRuleCollectionQuery collectionQuery = new RcRuleCollectionQuery();
    				RcRuleCollectionQuery.Criteria criteria = collectionQuery.createCriteria();
    				criteria.andClassifyIdEqualTo(rcClassification.getId().intValue());
    				List<RcRuleCollection> collectionList = rcRuleCollectionService.selectByExample(collectionQuery);
    				if (CollectionUtils.isNotEmpty(collectionList)) {
    					for (RcRuleCollection collection : collectionList) {
    						RcRuleCollection collectionNew = new RcRuleCollection();
    						collectionNew.setId(collection.getId());
    						collectionNew.setClassifyName(record.getClassifyName());
    						rcRuleCollectionService.updateByPrimaryKeySelective(collectionNew);
						}
    				}
    			}
    		}
    	}
        return 0;
    }

    @Override
    @Transactional
    public int updateByPrimaryKey(RcClassification record) {
        return rcClassificationMapper.updateByPrimaryKeySelective(record);
    }
}
